﻿package plugin.viproot
{
    import com.core.*;
    import com.greensock.*;
    import flash.display.*;
    import flash.events.*;
    import flash.external.*;
    import flash.filters.*;
    import manager.*;
    import morn.core.components.*;
    import morn.core.handlers.*;
    import plugin.newvip.model.*;
    import pluginfw.nio.*;
    import uiresource.vipui.*;
    import view.mornExtends.*;
    import view.uilt.*;

    public class VipRootCtrl extends VipRoot2UI
    {
        private var viproot:VipRoot;
        private var imgsp1:VipShowImg;
        private var imgsp2:VipShowImg;
        private var imgsp3:VipShowImg;
        private var imgarr:Array;
        private const SCALE:Number = 1;
        private const SCALEDA:Number = 1.42;
        private const XX:Number = -50;
        private var cfgviplv:Object;
        private var maskSP:Sprite;
        private var viplv:int;
        private var page:int = 0;
        private var firsPage:int;
        private var vipdata:Object;
        private var _imggw1:UrlImage;
        private var _imggw2:UrlImage;
        private var vipKid:int = 3;
        private var _filters:Array;
        private var imgBox:Box;
        private var timeline:TweenMax;
        private var tweenMax:TweenMax;

        public function VipRootCtrl(param1:VipRoot)
        {
            this.viproot = param1;
            return;
        }// end function

        override protected function initializeView() : void
        {
            super.initializeView();
            this.cfgviplv = ConfigDictionary.data.viplv;
            this.vipdata = ModelProxy.getValue("vip", "getvip");
            nameText.text = UserData.getUserData("name");
            var _loc_1:* = Control.create(VipRootControl, this);
            ModelProxy.addObserver("vip", _loc_1);
            this.imgBox = new Box();
            this.addChild(this.imgBox);
            this.imgBox.x = 90;
            this.imgBox.y = 300;
            this.imgsp1 = new VipShowImg();
            this.imgsp2 = new VipShowImg();
            this.imgsp3 = new VipShowImg();
            this.imgBox.addChild(this.imgsp1);
            this.imgBox.addChild(this.imgsp3);
            this.imgBox.addChild(this.imgsp2);
            this.imgsp1.x = this.XX + 150;
            this.imgsp2.x = this.XX + 355;
            this.imgsp3.x = this.XX + 560;
            this.imgarr = [this.imgsp1, this.imgsp2, this.imgsp3];
            this._filters = [new GlowFilter(16750597, 0.8, 16, 16, 2, 1, false)];
            this.maskSP = new Sprite();
            this.maskSP.graphics.beginFill(16777215, 1);
            this.maskSP.graphics.drawRect(0, 0, 441, 20);
            this.maskSP.graphics.endFill();
            this.maskSP.x = s9bar.x;
            this.maskSP.y = s9bar.y;
            var _loc_2:* = this.getChildIndex(s9bar);
            this.addChildAt(this.maskSP, (_loc_2 + 1));
            s9bar.mask = this.maskSP;
            this._imggw1 = new UrlImage();
            this._imggw2 = new UrlImage();
            this._imggw1.x = VIP.x + VIP.width - 8;
            this._imggw1.y = VIP.y;
            this._imggw1.width = VIP.width;
            this._imggw1.height = VIP.height;
            this._imggw2.x = vip2.x + vip2.width + 15;
            this._imggw2.y = vip2.y;
            this.addChild(this._imggw2);
            box1.addChild(this._imggw1);
            closeBtn.addEventListener(MouseEvent.CLICK, this.onclose);
            btn_chongzhi.addEventListener(MouseEvent.CLICK, this.shongzhi);
            this.updata();
            this.addEvernt();
            return;
        }// end function

        protected function shongzhi(event:MouseEvent) : void
        {
            if (!ItemUilt.canpay)
            {
                ModelProxy.change("movetips", "error", "功能尚未开放，敬请期待");
                return;
            }
            ExternalInterface.call("topay");
            return;
        }// end function

        protected function onclose(event:MouseEvent) : void
        {
            ModelProxy.change("plugin", "close", "vip");
            return;
        }// end function

        public function getVip(param1) : void
        {
            this.vipdata = param1;
            this.updata();
            return;
        }// end function

        private function updata() : void
        {
            var _loc_1:int = 0;
            if (this.vipdata == null)
            {
                return;
            }
            if (!this.cfgviplv)
            {
                return;
            }
            var _loc_2:String = "";
            if (this.vipdata.vipLevel == -1)
            {
                _loc_1 = int(this.cfgviplv[1].needpt);
                _loc_2 = "vip1";
            }
            else if (this.vipdata.vipLevel >= 0 && this.vipdata.vipLevel < 10)
            {
                _loc_1 = int(this.cfgviplv[(this.vipdata.vipLevel + 1)].needpt);
                _loc_2 = "vip" + (this.vipdata.vipLevel + 1);
            }
            else
            {
                _loc_1 = int(this.cfgviplv[this.vipdata.vipLevel].needpt);
                _loc_2 = "vip" + this.vipdata.vipLevel;
            }
            this.maskSP.width = this.vipdata.vipPoint / _loc_1 * 441;
            if (_loc_1 >= this.vipdata.vipPoint)
            {
                cyviptxt.text = "再充<font color =\'#FFCC59\'>" + (_loc_1 - this.vipdata.vipPoint) + "元宝</font>便可升为<font color =\'#FFCC59\'>" + _loc_2 + "</font>";
                lvtxt.text = this.vipdata.vipPoint + "/" + _loc_1;
            }
            else
            {
                cyviptxt.text = "再充<font color =\'#FFCC59\'>0元宝</font>便可升为<font color =\'#FFCC59\'>VIP10</font>";
                lvtxt.text = _loc_1 + "/" + _loc_1;
            }
            this.viplv = this.vipdata.vipLevel;
            if (this.viplv == 0)
            {
                this.page = 1;
                this.noVip();
                this.setPage();
            }
            else if (this.viplv == -1)
            {
                this.page = -1;
                this.vipLV0();
                this.setPage();
            }
            else
            {
                if (this.firsPage != this.viplv)
                {
                    this.page = this.viplv;
                    this.firsPage = this.page;
                    this.setPage();
                }
                else
                {
                    this.initlingqubtn();
                }
                this.vipLV();
            }
            this.showList();
            return;
        }// end function

        private function vipLV() : void
        {
            if (this.viplv < 10)
            {
                vip2.url = "cs2_viproot/" + this.viplv + ".png";
                VIP.url = "cs2_viproot/" + this.viplv + ".png";
                var _loc_1:Boolean = false;
                this._imggw1.visible = false;
                this._imggw2.visible = _loc_1;
            }
            else
            {
                vip2.url = "cs2_viproot/1.png";
                VIP.url = "cs2_viproot/1.png";
                var _loc_1:* = "cs2_viproot/" + (this.vipdata.vipLevel - 10) + ".png";
                this._imggw1.url = "cs2_viproot/" + (this.vipdata.vipLevel - 10) + ".png";
                this._imggw2.url = _loc_1;
                var _loc_1:Boolean = true;
                this._imggw1.visible = true;
                this._imggw2.visible = _loc_1;
            }
            vipimg.url = "cs2_viproot/vip.png";
            this.setTweenMax();
            vipbuff_txt.label = "领取VIP" + this.viplv + "经验BUFF";
            return;
        }// end function

        private function vipLV0() : void
        {
            if (this.vipdata.vipExpCardEndTime > 0)
            {
                var _loc_1:Boolean = false;
                vip12.visible = false;
                vip1.visible = _loc_1;
                vipimg.url = "cs2_viproot/tiyin.png";
                vip2.url = "cs2_viproot/0.png";
                vipbuff_txt.label = "领取VIP体验经验BUFF";
                this.setTweenMax();
            }
            return;
        }// end function

        private function noVip() : void
        {
            vipimg.url = "cs2_viproot/vip.png";
            vip12.visible = false;
            vipbuff_txt.label = "领取VIP经验BUFF";
            return;
        }// end function

        private function setTweenMax() : void
        {
            if (this.vipdata.vipDayBuff == 0)
            {
                this.cleartimeline();
                vipbuff_txt.labelColors = "0x54B436,0xefd742,0x54b436,0xefd742";
                this.timeline = TweenMax.to(vipbuff_txt, 1, {glowFilter:{color:16763904, alpha:1, blurX:15, blurY:15}, repeat:10000, yoyo:true});
            }
            else
            {
                vipbuff_txt.labelColors = "0x666565";
                this.cleartimeline();
            }
            return;
        }// end function

        private function cleartimeline() : void
        {
            if (this.timeline)
            {
                this.timeline.kill();
                vipbuff_txt.filters = [];
            }
            return;
        }// end function

        public function showList() : void
        {
            if (this.page <= 0)
            {
                list.array = ItemUilt.getBoxArr(this.cfgviplv[1].levelgift);
            }
            else
            {
                list.array = ItemUilt.getBoxArr(this.cfgviplv[this.page].levelgift);
            }
            list.renderHandler = new Handler(this.listHandler);
            return;
        }// end function

        private function listHandler(param1:Component, param2:int) : void
        {
            if (param2 < list.array.length)
            {
                (param1 as FlashItem).data = list.array[param2];
            }
            return;
        }// end function

        private function addEvernt() : void
        {
            var _loc_1:int = 0;
            while (_loc_1 < this.imgarr.length)
            {
                
                this.imgarr[_loc_1].addEventListener(MouseEvent.ROLL_OUT, this.outtips);
                this.imgarr[_loc_1].addEventListener(MouseEvent.ROLL_OVER, this.onOver);
                _loc_1++;
            }
            lingqubtn.addEventListener(MouseEvent.CLICK, this.lingqu);
            btn_right.addEventListener(MouseEvent.CLICK, this.onRight);
            btn_left.addEventListener(MouseEvent.CLICK, this.onLeft);
            vipbuff_txt.addEventListener(MouseEvent.CLICK, this.vipBuff);
            vipbuff_txt.addEventListener(MouseEvent.ROLL_OVER, this.bufftips);
            vipbuff_txt.addEventListener(MouseEvent.ROLL_OUT, this.outtips);
            return;
        }// end function

        protected function outtips(event:MouseEvent) : void
        {
            ModelProxy.change("tips", "state", "close");
            return;
        }// end function

        protected function bufftips(event:MouseEvent) : void
        {
            var _loc_3:Array = null;
            var _loc_4:String = null;
            var _loc_2:String = "";
            if (this.viplv == 0)
            {
                _loc_2 = this.cfgviplv[1].expshow;
            }
            else
            {
                _loc_2 = this.cfgviplv[this.viplv].expshow;
            }
            switch(int(_loc_3[0]))
            {
                case 2:
                {
                    break;
                }
                case 3:
                {
                    break;
                }
                case 4:
                {
                    break;
                }
                default:
                {
                    break;
                }
            }
            return;
        }// end function

        protected function vipBuff(event:MouseEvent) : void
        {
            PluginManager.instance.sendToBufferServer("vip", CMDVip.LingQu, new Output());
            return;
        }// end function

        protected function buyVIPK(event:MouseEvent) : void
        {
            var _loc_2:* = new Output();
            _loc_2.writeVarInt(this.vipKid, false);
            PluginManager.instance.sendToBufferServer("vip", CMDVip.LingQuFirst, _loc_2);
            return;
        }// end function

        protected function onLeft(event:MouseEvent) : void
        {
            if (this.viplv == -1)
            {
                this.page = -1;
            }
            else
            {
                if (this.page <= 0)
                {
                    return;
                }
                var _loc_2:String = this;
                var _loc_3:* = this.page - 1;
                _loc_2.page = _loc_3;
                if (this.page <= 0)
                {
                    this.page = 1;
                }
            }
            this.setPage();
            return;
        }// end function

        protected function onRight(event:MouseEvent) : void
        {
            if (this.viplv == -1)
            {
                this.page = 1;
            }
            else
            {
                if (this.page == 10)
                {
                    return;
                }
                var _loc_2:String = this;
                var _loc_3:* = this.page + 1;
                _loc_2.page = _loc_3;
                if (this.page >= (this.viplv + 1))
                {
                    this.page = this.viplv + 1;
                    btn_right.disabled = true;
                }
            }
            this.setPage();
            return;
        }// end function

        private function setPage() : void
        {
            if (this.viplv == -1)
            {
                var _loc_1:* = this.page == 1;
                btn_right.disabled = this.page == 1;
                btn_left.visible = _loc_1;
                if (this.page == -1)
                {
                    vipimg.url = "cs2_viproot/tiyin.png";
                    vip1.visible = false;
                }
                else
                {
                    vipimg.url = "cs2_viproot/vip.png";
                    vip1.url = "cs2_viproot/1.png";
                    vip1.visible = true;
                }
                lingqubtn.visible = false;
            }
            else
            {
                vip1.visible = true;
                if (this.page < 10)
                {
                    var _loc_1:* = "cs2_viproot/" + this.page + ".png";
                    vip1.url = "cs2_viproot/" + this.page + ".png";
                    VIP.url = _loc_1;
                    this._imggw1.visible = false;
                    vip12.visible = false;
                }
                else
                {
                    var _loc_1:String = "cs2_viproot/1.png";
                    vip1.url = "cs2_viproot/1.png";
                    VIP.url = _loc_1;
                    var _loc_1:* = "cs2_viproot/" + this.page % 10 + ".png";
                    vip12.url = "cs2_viproot/" + this.page % 10 + ".png";
                    this._imggw1.url = _loc_1;
                    vip12.visible = true;
                    this._imggw1.visible = true;
                }
                if (this.viplv < 10)
                {
                    btn_right.disabled = this.page >= (this.viplv + 1);
                }
                else
                {
                    btn_right.disabled = this.page >= this.viplv;
                }
                btn_left.visible = this.page > 1;
                this.initlingqubtn();
            }
            ShowVip.initShow(this.page, this.imgsp1, this.imgsp2, this.imgsp3);
            this.showList();
            return;
        }// end function

        private function initlingqubtn() : void
        {
            if (this.vipdata.vipLevelGift)
            {
                if (this.vipdata.vipLevelGift.indexOf(this.page) != -1)
                {
                    lingqubtn.visible = false;
                }
                else
                {
                    lingqubtn.visible = true;
                }
            }
            else
            {
                lingqubtn.visible = false;
            }
            if (this.vipdata.vipLevel != -1 && !this.vipdata.vipLevelGift[this.page] && this.page > this.vipdata.vipLevel)
            {
                lingqubtn.disabled = true;
            }
            else
            {
                lingqubtn.disabled = false;
            }
            return;
        }// end function

        protected function lingqu(event:MouseEvent) : void
        {
            var _loc_2:* = new Output();
            _loc_2.writeVarInt(this.page, false);
            PluginManager.instance.sendToBufferServer("vip", CMDVip.LingQuZhuanshu, _loc_2);
            return;
        }// end function

        protected function outimg(event:MouseEvent) : void
        {
            var _loc_2:* = event.currentTarget as Sprite;
            switch(_loc_2)
            {
                case this.imgsp1:
                {
                    TweenMax.to(this.imgsp1, 2, {scaleX:this.SCALE, scaleY:this.SCALE});
                    this.imgBox.setChildIndex(this.imgsp1, 0);
                    break;
                }
                case this.imgsp2:
                {
                    TweenMax.to(this.imgsp2, 2, {scaleX:this.SCALE, scaleY:this.SCALE});
                    this.imgBox.setChildIndex(this.imgsp2, 0);
                    break;
                }
                case this.imgsp3:
                {
                    TweenMax.to(this.imgsp3, 2, {scaleX:this.SCALE, scaleY:this.SCALE});
                    break;
                }
                default:
                {
                    break;
                }
            }
            return;
        }// end function

        protected function onOver(event:MouseEvent) : void
        {
            var _loc_4:Array = null;
            var _loc_2:* = event.currentTarget as Sprite;
            if (this.tweenMax)
            {
                this.tweenMax = null;
            }
            var _loc_3:String = "";
            if (this.cfgviplv[this.page].tips)
            {
                _loc_4 = (this.cfgviplv[this.page].tips as String).split("#");
            }
            switch(_loc_2)
            {
                case this.imgsp1:
                {
                    if (_loc_4[0])
                    {
                        _loc_3 = _loc_4[0];
                    }
                    this.tweenMax = TweenMax.to(this.imgsp1, 0.3, {scaleX:this.SCALEDA, scaleY:this.SCALEDA});
                    this.tweenMax = TweenMax.to(this.imgsp3, 0.3, {scaleX:this.SCALE, scaleY:this.SCALE});
                    this.tweenMax = TweenMax.to(this.imgsp2, 0.3, {scaleX:this.SCALE, scaleY:this.SCALE});
                    this.imgBox.setChildIndex(this.imgsp1, 2);
                    break;
                }
                case this.imgsp2:
                {
                    if (_loc_4[1])
                    {
                        _loc_3 = _loc_4[1];
                    }
                    this.tweenMax = TweenMax.to(this.imgsp2, 0.3, {scaleX:this.SCALEDA, scaleY:this.SCALEDA});
                    this.tweenMax = TweenMax.to(this.imgsp3, 0.3, {scaleX:this.SCALE, scaleY:this.SCALE});
                    this.tweenMax = TweenMax.to(this.imgsp1, 0.3, {scaleX:this.SCALE, scaleY:this.SCALE});
                    this.imgBox.setChildIndex(this.imgsp2, 2);
                    break;
                }
                case this.imgsp3:
                {
                    if (_loc_4[2])
                    {
                        _loc_3 = _loc_4[2];
                    }
                    this.tweenMax = TweenMax.to(this.imgsp3, 0.3, {scaleX:this.SCALEDA, scaleY:this.SCALEDA});
                    this.tweenMax = TweenMax.to(this.imgsp2, 0.3, {scaleX:this.SCALE, scaleY:this.SCALE});
                    this.tweenMax = TweenMax.to(this.imgsp1, 0.3, {scaleX:this.SCALE, scaleY:this.SCALE});
                    this.imgBox.setChildIndex(this.imgsp3, 2);
                    break;
                }
                default:
                {
                    break;
                }
            }
            ModelProxy.change("tips", "normal", _loc_3);
            return;
        }// end function

    }
}
